<?php

include_once "BabiliUsersInterface.php";

/**
 * We connect Babili to bonitasoft.org users
 */

class BabiliUserSystem implements BabiliUsersInterface{
	
	//the db connector
	protected $db;
	
	/**
	 * The construct load the db connector
	 * @param $db
	 */
	public function __construct($db){
		$this->db = $db;
	}
	
	/**
	 * Function that says if a user is logged or not
	 * @return bool
	 */
	public function babili_user_is_logged_in(){
		if(!empty($_SESSION['babili_user']))
			return true;
		else
			return false;
	}
	
	/**
	 * Function that returns the id of the current user
	 * @return int the user id
	 */
	public function babili_user_get_id(){
		if(!empty($_SESSION['babili_user']['id']))
			return $_SESSION['babili_user']['id'];
		else
			return null;
	}
	
	/**
	 * Function which have to give us information about the user (userid, username, email)
	 * @param $userid the id of the user
	 * @return array $user = array('userid' => $USERID, 'email' => $USERMAIL)
	 * 		   null if we don't find it
	 */
	public function babili_user_get($userid){
		//if it's the current user, return the session
		if($this->babili_user_get_id() == $userid){
			return $_SESSION['babili_user'];
		}else{
			$query = "SELECT * FROM user WHERE id = :userid";
			$query_args = array(':userid' => $userid);
			$result = $this->db->select($query, $query_args);
			if(isset($result[0])){
				return $result[0];
			}else{
				return null;
			}
		}

	}
	
	/**
	 * Give all Babili users
	 * @return array $users = array($USERID => array('username' => $USERNAME, 'userid' => $USERID, 'email' => $USERMAIL),
	 * 								$USERID2 => array('username' => $USERNAME2, 'userid' => $USERID2, 'email' => $USERMAIL2),
	 * 								...)
	 */
	public function babili_user_get_all(){
		
		$query = "SELECT * FROM user";
		return $this->db->select($query);
		
	}
	
	/**
	 * Display or send to the login form
	 * @return the display of the login form or redirect to it
	 */
	public function babili_user_login_form(){
		
	}
	
	/**
	 * Logout the user
	 * @return or redirect to the logout page
	 */
	public function babili_user_logout(){
		
		unset($_SESSION['babili_user']);
		return true;
		
	}
	
	/**
	 * Display the search form of users
	 * @return the html display of the search form
	 */
	public function babili_user_search_form(){
		
	}
	
	/**
	 * Return an array of users, results of the search
	 * @param array of search paramaters (username, email...)
	 * @return array $users = array($USERID => array('username' => $USERNAME, 'userid' => $USERID, 'email' => $USERMAIL),
	 * 								$USERID2 => array('username' => $USERNAME2, 'userid' => $USERID2, 'email' => $USERMAIL2),
	 * 								...)
	 */
	public function babili_user_search_result($params = array()){
		
		//we need params or we can't search
		if(empty($params))
			return null;
		
		$query = "SELECT * FROM user WHERE ";
		//init query args
		$query_args = array();
		
		foreach($params as $key => $param){
			//The query is for example : email = :email
			$query .= $key . ' LIKE :' . $key;
			//$query_args[':email'] = '%test@babili.net%';
			$query_args[':' . $key] = '%' . $param . '%';
			
			return $this->db->select($query, $query_args);
		}
	}
}